package com.company.dcm.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.company.dcm.common.R;
import com.company.dcm.constant.RoleConstant;
import com.company.dcm.entity.Dev;
import com.company.dcm.entity.R_DeviceInfo;
import com.company.dcm.entity.User;
import com.company.dcm.entity.WorkShop;
import com.company.dcm.service.*;
import com.company.dcm.utils.IdUtils;
import com.company.dcm.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dev")
public class DevController {
    @Autowired
    private DevService devService;
    @Autowired
    private WorkShopService workShopService;
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private R_DeviceInfoService rDeviceInfoService;

    @SaCheckRole(RoleConstant.WORKSHOP_MANAGER)
    @PostMapping("/add")
    public R add(@RequestBody Dev dev){
//        if(StringUtils.isNullOrEmpty(dev.getDeviceCode())|StringUtils.isNullOrEmpty(dev.getName()))return R.error("参数不全！");
//
//        List<WorkShop> workShops = workShopService.getBaseMapper().selectList(new LambdaQueryWrapper<WorkShop>()
//                .eq(WorkShop::getName,dev.getName()));
//        List<String> workShopsList = StpUtil.getPermissionList();
//
//        for (WorkShop workShop:workShops){
//            for (String workShopId:workShopsList) {
//                if(workShopId.equals(workShop.getId().toString())){
//                    dev.setId(workShop.getId());
//                    break;
//                }
//            }
//        }
//        if(dev.getId()==null)return R.error("车间错误！");
//
//        dev.setDevId(IdUtils.getInstance().NextId());
//        if(devService.count(new LambdaQueryWrapper<Dev>().eq(Dev::getDeviceCode,dev.getDeviceCode()).eq(Dev::getId,dev.getId()))>0){
//            return R.error("该机台已存在！");
//        }
//        if(devService.save(dev)){
//            return R.success("新增成功！");
//        }
//        return R.error("未知错误！");
        if(StringUtils.isNullOrEmpty(dev.getDeviceCode())| dev.getId() == null)return R.error("参数不全！");

        List<WorkShop> workShops = workShopService.getBaseMapper().selectList(new LambdaQueryWrapper<WorkShop>()
                .eq(WorkShop::getId,dev.getId()));
        List<String> workShopsList = StpUtil.getPermissionList();

        boolean flag=false;
        for (WorkShop workShop:workShops){
            for (String workShopId:workShopsList) {
                if(workShopId.equals(workShop.getId().toString())){
                    flag = true;
                    break;
                }
            }
        }
        if(!flag)return R.error("车间错误！");

        dev.setDevId(IdUtils.getInstance().NextId());
        if(devService.count(new LambdaQueryWrapper<Dev>().eq(Dev::getDeviceCode,dev.getDeviceCode()).eq(Dev::getId,dev.getId()))>0){
            return R.error("该机台已存在！");
        }
        if(devService.count(new LambdaQueryWrapper<Dev>().eq(Dev::getDeviceCode,dev.getDeviceCode()))>0){
            return R.error("该机台在其他车间重名！");
        }
        if(devService.save(dev)){
            return R.success("新增成功！");
        }
        return R.error("未知错误！");
    }

    @SaCheckRole(RoleConstant.WORKSHOP_MANAGER)
    @PostMapping("/delete")
    public R delete(@RequestBody Dev dev){
        if(StringUtils.isNullOrEmpty(dev.getDeviceCode())| dev.getId() == null)return R.error("参数不全！");

        List<WorkShop> workShops = workShopService.getBaseMapper().selectList(new LambdaQueryWrapper<WorkShop>()
                .eq(WorkShop::getId,dev.getId()));
        List<String> workShopsList = StpUtil.getPermissionList();

        for (WorkShop workShop:workShops){
            for (String workShopId:workShopsList) {
                if(workShopId.equals(workShop.getId().toString())){
                    dev.setId(workShop.getId());
                    break;
                }
            }
        }
        if(dev.getId()==null)return R.error("车间错误！");


        if(devService.count(new LambdaQueryWrapper<Dev>().eq(Dev::getDeviceCode,dev.getDeviceCode()).eq(Dev::getId,dev.getId()))==0){
            return R.error("该机台不存在！");
        }
        if(devService.remove(new LambdaQueryWrapper<Dev>().eq(Dev::getDeviceCode,dev.getDeviceCode()).eq(Dev::getId,dev.getId()))){
            return R.success("删除成功！");
        }
        return R.error("未知错误！");
    }

    @GetMapping("/list")
    public R list(Long id){
        List<Dev> devList=new ArrayList<>();
        if(id==null){
            User user = userService.getExistByUserName((String) StpUtil.getLoginId());
            List<Long> workShopIds= permissionService.getWorkShopIdsByUserId(user.getUserId());

            for (Long workShopId:workShopIds){
                List<Dev> devs=devService.getBaseMapper().selectList(new LambdaQueryWrapper<Dev>().eq(Dev::getId,workShopId));
                devList.addAll(devs);
            }
        }else {
            List<Dev> devs=devService.getBaseMapper().selectList(new LambdaQueryWrapper<Dev>().eq(Dev::getId,id));
            devList.addAll(devs);
        }

        for (Dev dev:devList) {
            dev.setWorkShopName(workShopService.getBaseMapper().selectById(dev.getId()).getName());
        }

        return R.success(devList);
    }

    @GetMapping("/getDev")
    public R getDev(@RequestParam("devId") String devId){
        R_DeviceInfo dev = rDeviceInfoService.getById(devId);

        return R.success(dev);
    }


}
